What is http-deceiver?
The http-deceiver npm package is designed to manipulate HTTP requests and responses. It allows developers to intercept, modify, and create custom behaviors for HTTP traffic, which is particularly useful in testing and development environments where simulating various network conditions and responses is necessary.
What are http-deceiver's main functionalities?
Intercept and modify HTTP requests
This feature allows you to intercept HTTP requests and modify properties such as the URL before the server processes them. In the provided code, the request URL is changed to '/modified-path', and a custom response is sent back.
const httpDeceiver = require('http-deceiver');
const http = require('http');
const server = http.createServer((req, res) => {
httpDeceiver.deceive(req, res, (req, res) => {
req.url = '/modified-path';
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Request was modified');
});
});
server.listen(3000);
Simulate server responses
This feature is useful for simulating different server responses under various conditions. In this example, a 503 Service Unavailable response is simulated regardless of the actual server state or the incoming request.
const httpDeceiver = require('http-deceiver');
const http = require('http');
const server = http.createServer((req, res) => {
httpDeceiver.deceive(req, res, (req, res) => {
res.writeHead(503, {'Content-Type': 'text/plain'});
res.end('Service Unavailable');
});
});
server.listen(3000);
Other packages similar to http-deceiver
nock
Nock is a powerful HTTP server mocking and expectations library for Node.js. It allows you to control HTTP requests and responses with a high level of granularity. Compared to http-deceiver, nock provides a more comprehensive API for defining precise request and response behaviors, making it ideal for complex testing scenarios.
sinon
Sinon provides standalone test spies, stubs, and mocks for JavaScript. While it is not limited to HTTP, it can be used in conjunction with other libraries to mock HTTP interactions. Sinon is generally more flexible than http-deceiver as it can handle a wider range of testing needs beyond just HTTP.
HTTP Deceiver
Deceive!
LICENSE
This software is licensed under the MIT License.
Copyright Fedor Indutny, 2015.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.